﻿@page "/blazor/examples/page-stack/page2/{id}"
@layout PageStackLayout
@inject PageStackNavController NavController

<PStackPageBar Title="Page 2" />

<p class="caption">url: /blazor/examples/page-stack/page2/
    @if (_loading)
    {
        <MProgressCircular Indeterminate Size="16"/>
    }
    else
    {
        <span>@Id</span>
    }
</p>

<div>
    <p class="text-h6">Page 2 created at @(_pageCreatedAt.ToString("HH:mm:ss zz"))</p>

    <div>
        <a class="text-decoration-underline" @onclick="UpdateId">Update Id</a>
    </div>

    <a class="text-decoration-underline"
       @onclick="@(() => NavController.Push("/blazor/examples/page-stack/page3"))">
        Navigate to Page 3
    </a>

    <MSyntaxHighlight Code="@sourceCode"
                      Language="razor"
                      Style="font-size: 10px">
    </MSyntaxHighlight>
</div>

@code {

    [Parameter] public string Id { get; set; } = default!;

    private DateTimeOffset _pageCreatedAt;

    private string? _prevId;
    private bool _loading;

    protected override void OnInitialized()
    {
        _pageCreatedAt = DateTimeOffset.UtcNow;
    }

    protected override async Task OnParametersSetAsync()
    {
        if (_prevId != Id)
        {
            _loading = true;
            StateHasChanged();

            await Task.Delay(1000);
            _loading = false;
            _prevId = Id;
        }
    }

    private void UpdateId()
    {
        var newId = Random.Shared.Next(100, 999);
        NavController.Replace($"/blazor/examples/page-stack/page2/{newId}");
    }

}